import request from '@/utils/request'

// 新增设备
export const saveDevice = async (data) => {
  try {
    const response = await request({
      url: '/device/save',
      method: 'post',
      data, // 传递设备数据
    })
    console.log('新增设备成功:', response)
    return response
  } catch (error) {
    console.error('新增设备失败:', error)
    throw error
  }
}

// 获取设备列表
export const getDeviceList = async (params) => {
  try {
    const response = await request({
      url: '/device/select',
      method: 'get',
      params: {
        name: params.name || '',
        code: params.code || '',
        type: params.type === 'true' ? true : (params.type === 'false' ? false : ''),
        status: params.status === 'true' ? true : (params.status === 'false' ? false : ''),
        pageNum: params.pageNum || 1,
        pageSize: params.pageSize || 10
      }
    })
    return response
  } catch (error) {
    console.error('获取设备列表失败:', error)
    throw error
  }
}

// 编辑设备
export const updateDevice = async (data) => {
  try {
    const response = await request({
      url: '/device/update',
      method: 'put',
      data,
    })
    return response
  } catch (error) {
    throw error
  }
}

// 删除设备
export const deleteDevice = async (id) => {
  return await request({
    url: '/device/del',
    method: 'get',
    params: { id }
  })
}

// 批量删除设备
export const batchDeleteDevice = async (ids) => {
  return await request({
    url: '/device/batch',
    method: 'post',
    data: ids
  })
}
